<template>
  <el-dialog v-if="load" title="完善补货信息" v-model="visible" append-to-body :close-on-click-modal="false" width="800px" :z-index="2000"
    @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top">
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="物流方式" prop="lc_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.lc_id" placeholder="请选择" :options="options.logisticsOptions" class="w-full"
            @change="({ option }) => (form.logistics_name = option.label)" />
        </el-form-item>
        <el-form-item label="运单号" prop="logistics_number"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.logistics_number" placeholder="请输入" class="w-full" />
        </el-form-item>
        <el-form-item label="预计到货时间">
          <el-date-picker v-model="form.except_deliver_time" placeholder="日期选择" type="date" value-format="YYYY-MM-DD"
            class="w-full" />
        </el-form-item>
        <el-form-item label="sku">
          <el-input v-model="form.sku" disabled placeholder="请输入" class="w-full" />
        </el-form-item>
        <el-form-item label="补货数量">
          <el-input v-model="form.process_num" disabled placeholder="请输入" class="w-full" />
        </el-form-item>
        <div class="col-span-full">是否确认通知仓库等待补货？</div>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const formRef = ref()

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (row) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  const { a_id, sku, process_num } = row
  form.value = { a_id, sku, process_num, except_deliver_time: '' }
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async () => {
  if (!await formRef?.value.validate()) return
  loadings.submit = true
  api.replenishment(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>